package com.jinlia.show.modules.base.system.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.jinlia.show.common.core.entity.BaseEntity;
import com.jinlia.show.common.core.utils.CacheUtils;
import com.jinlia.show.modules.base.config.DynamicSchemaConfig;
import com.jinlia.show.modules.base.constant.DeliveryTypeEnum;
import com.jinlia.show.modules.base.constant.OrderPayStatusEnum;
import com.jinlia.show.modules.base.constant.OrderStatusEnum;
import com.jinlia.show.modules.base.constant.i.IBaseEnum;
import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDate;

@Data
@TableName(value = "erp_order", schema = DynamicSchemaConfig.schema_system)
public class Order extends BaseEntity {
    @TableId()
    private Long id;
    /**
     * 应用id
     */
    private String appId;
    private Long companyId;
    /**
     * 顾客id
     */
    private Long userId;

    private Long memberCardId;
    /**
     * 接单人->接单之后自动打印
     */
    private Long confirmUserId;

    /**
     * 订单号
     */
    private String orderNum;
    /**
     * 订单日期
     */
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
    @JsonDeserialize(using = LocalDateDeserializer.class)
    private LocalDate orderDate;
    /**
     * 原始价格 = 配送费 + 折扣后金额 + 抹零金额
     */
    private BigDecimal originalMoney;
    /**
     * 折扣金额（originalMoney*(1-discountRate)）
     * 默认为 0
     */
    private BigDecimal discountMoney;

    //抹零金额 = 折扣后金额 - 应收金额
    private BigDecimal mlMoney;
    //应收金额 = 折扣后金额 - 抹零金额
    private BigDecimal receivableMoney;
    //已收金额 默认 0
    private BigDecimal receivedMoney;
    //已退金额 默认 0
    private BigDecimal refundedMoney;
    //配送费
    private BigDecimal deliveryMoney;
    //配送方式  DeliveryTypeEnum 1-自提，2-配送待确认，2-外配，3-自配
    private Integer deliveryType;
    @TableField(exist = false)
    private String deliveryTypeName;
    /**
     * 收款状态
     * 收款状态10:未收，11:部分收款，12:全部收款,13部分退款，14全部退款,15退款中
     */
    private Integer payStatus;
    /**
     * 订单状态:0-正常，1-已取消,3-申请售后
     */
    private Integer enableStatus;
    /**
     * 订单备注
     */
    private String remark;
    /**
     * 订单的状态 OrderStatusEnum
     */
    private Integer orderStatus;

    @TableField(exist = false)
    private String payStatusName;

    public void setPayStatus(Integer payStatus) {
        this.payStatus = payStatus;
        this.payStatusName = IBaseEnum.getLabelByValue(payStatus, OrderPayStatusEnum.class);
    }

    public void setDeliveryType(Integer deliveryType) {
        this.deliveryType = deliveryType;
        this.deliveryTypeName = IBaseEnum.getLabelByValue(deliveryType, DeliveryTypeEnum.class);
    }

    @TableField(exist = false)
    private String orderStatusName;

    public void setOrderStatus(Integer orderStatus) {
        this.orderStatus = orderStatus;
        this.orderStatusName = IBaseEnum.getLabelByValue(orderStatus, OrderStatusEnum.class);
    }

    @TableField(exist = false)
    private String userName;

    public void setUserId(Long userId) {
        this.userId = userId;
        this.userName = CacheUtils.fromTable("nick_name", "wxapp_user", userId);
    }
    @TableField(exist = false)
    private String confirmUserName;
    public void setConfirmUserId(Long confirmUserId) {
        this.confirmUserId = confirmUserId;
        this.confirmUserName = CacheUtils.fromTable("nickname", "sys_user", confirmUserId);
    }
}
